package com.lgx.mall.core.entity;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import net.sf.json.JSONObject;

import com.lgx.common.entity.IdGenerator;
import com.lgx.common.web.util.DictUtils;

/**
 * 商品
 * 
 * @author hezhuo
 *
 */
@Entity
@Table(name = "TM_PRODUCT")
public class Product implements Serializable {
	
	public static final int ONSALE = 1;
	
	public static final int OFFSALE = 0;

	/**
	 * 
	 */
	private static final long serialVersionUID = 3052777992943648489L;

	@Id
	@GeneratedValue(strategy=GenerationType.TABLE,generator=IdGenerator.PRODUCT_ID_GENERATOR)
	@TableGenerator(
			name = IdGenerator.PRODUCT_ID_GENERATOR,
			table = IdGenerator.GENERATOR_TABLE,
			pkColumnName = IdGenerator.GEN_NAME,
			pkColumnValue = IdGenerator.PRODUCT_ID_GENERATOR,
			valueColumnName = IdGenerator.GEN_VALUE,
			initialValue = 1,
			allocationSize = 1
	)
	private long id;
	
	/**
	 * 商品名称
	 */
	private String name;
	
	/**
	 * 商品url地址
	 */
	private String url = "";
	
	/**
	 * 单价
	 */
	private Double price;
	
	/**
	 * 库存量
	 */
	private Integer leaves = 0;
	
	/**
	 * 上架总量
	 */
	private Integer amount = 0;
	
	/**
	 * 商品描述
	 */
	private String description;
	
	/**
	 * 商品所属品牌
	 */
	@ManyToOne
	@JoinColumn(name = "brand_id")
	private Brand brand;
	
	/**
	 * 图片地址1
	 */
	private String imageUrl1;
	
	/**
	 * 图片地址2
	 */
	private String imageUrl2;
	
	/**
	 * 是否在售，1在售，0下架
	 */
	private int onsale = ONSALE;
	
	/**
	 * 所属分区
	 */
	private int areaCategory;
	
	/**
	 * 所属商品类别
	 */
	@ManyToOne
	@JoinColumn(name = "category_id")
	private Category category;
	
	/**
	 * 可选颜色列表，用分号隔开
	 */
	private String color = "";
	
	/**
	 * 可选衣服尺码，用分号隔开
	 */
	private String clothessize = "";
	
	/**
	 * 可选鞋子尺码，用分号隔开
	 */
	private String shoesize = "";
	
	/**
	 * 可选运费支付方式，用分号隔开
	 */
	private String fareway = "";
	
	private Date createdate;
	
	/**
	 * 同城地
	 */
	private String areaNode;
	
	/**
	 * 重量
	 */
	private Double weight = 0d;
	
	/**
	 * 配送说明
	 */
	private String farewaydesc = "";
	
	/**
	 * 商品编号
	 */
	private String code;
	
	private String isdelete = "0";
	
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getFarewaydesc() {
		return farewaydesc;
	}

	public void setFarewaydesc(String farewaydesc) {
		this.farewaydesc = farewaydesc;
	}

	public String getSaledesc() {
		return saledesc;
	}

	public void setSaledesc(String saledesc) {
		this.saledesc = saledesc;
	}

	/**
	 * 售后说明
	 */
	private String saledesc = "";

	public String getAreaNode() {
		return areaNode;
	}

	public void setAreaNode(String areaNode) {
		this.areaNode = areaNode;
	}

	public Double getWeight() {
		if(weight==null) {
			return 0d;
		}
		return weight;
	}

	public void setWeight(Double weight) {
		this.weight = weight;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Integer getLeaves() {
		return leaves;
	}

	public void setLeaves(Integer leaves) {
		this.leaves = leaves;
	}

	public Integer getAmount() {
		return amount;
	}

	public void setAmount(Integer amount) {
		this.amount = amount;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public String getImageUrl1() {
		return imageUrl1;
	}

	public void setImageUrl1(String imageUrl1) {
		this.imageUrl1 = imageUrl1;
	}

	public String getImageUrl2() {
		return imageUrl2;
	}

	public void setImageUrl2(String imageUrl2) {
		this.imageUrl2 = imageUrl2;
	}

	public int getOnsale() {
		return onsale;
	}

	public void setOnsale(int onsale) {
		this.onsale = onsale;
	}

	public int getAreaCategory() {
		return areaCategory;
	}

	public void setAreaCategory(int areaCategory) {
		this.areaCategory = areaCategory;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public String getColor() {
		if(color==null) {
			return "";
		}
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getClothessize() {
		if(clothessize==null) {
			return "";
		}
		return clothessize;
	}

	public void setClothessize(String clothessize) {
		this.clothessize = clothessize;
	}

	public String getShoesize() {
		if(shoesize==null) {
			return "";
		}
		return shoesize;
	}

	public void setShoesize(String shoesize) {
		this.shoesize = shoesize;
	}

	public String getFareway() {
		if(fareway==null) {
			return "";
		}
		return fareway;
	}

	public void setFareway(String fareway) {
		this.fareway = fareway;
	}

	public Date getCreatedate() {
		return createdate;
	}

	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}

	public Integer getSale() {
		return amount - leaves;
	}

	public String getIsdelete() {
		return isdelete;
	}

	public void setIsdelete(String isdelete) {
		this.isdelete = isdelete;
	}
	
	@Override
	public String toString() {
		JSONObject obj = new JSONObject();
		
		obj.put("id", this.getId());
		obj.put("code", this.getCode());
		obj.put("imageUrl1", this.getImageUrl1());
		obj.put("name", this.getName());
		obj.put("areaCategory", DictUtils.getDictNameByKindAndCode(DictUtils.PRODUCT_AREA, this.getAreaCategory()+""));
		obj.put("category", this.getCategory().getName());
		obj.put("brand", this.getBrand() != null?this.getBrand().getName():"");
		return obj.toString();
	}

}
